# json模块实际操作  爬取4399小游戏
import requests
import json
from lxml import etree

data_list = []
for i in range(1, 11):
    if i == 1:
        url = "https://www.4399.com/flash/"

    else:
        url = f"https://www.4399.com/flash/new_{i}.htm" # 这里一定要加一个f

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
        "Cookie": "home4399=yes; UM_distinctid=19385f31e9a8c7-0b8e2292d4d346-4c657b58-1fa400-19385f31e9b1429; Hm_lvt_334aca66d28b3b338a76075366b2b9e8=1733119123; HMACCOUNT=A857567457C6F4F7; global_hs=4399.com%7C%7C%7C4399%u751F%u6B7B%u72D9%u51FB%7C%7Chttps%3A//ssjj.4399.com/%7C%7C0; _4399stats_vid=17331191277274710; USESSIONID=12ec172d-aea6-4ddd-b288-78254acb9d08; phlogact=l121528; _4399tongji_vid=173311912836197; _4399tongji_st=1733119128; _gprp_c=\\; CNZZDATA30039538=cnzz_eid%3D730271404-1733119123-https%253A%252F%252Fcn.bing.com%252F%26ntime%3D1733119711; Hm_lpvt_334aca66d28b3b338a76075366b2b9e8=1733119711"
    }
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding
    # print(response.text)

    html_obj = etree.HTML((response.text))
    li_list = html_obj.xpath('//ul[@class="n-game cf"]/li')
    # print(li_list)

    for li in li_list:
        item = {}
        item["href"] = li.xpath('./a/@href')[0]
        item["title"] = li.xpath('./a/b/text()')[0]
        # print(item)
        data_list.append(item)

print(data_list)
print(len(data_list))

with open("data.json", "w", encoding="utf-8") as f:
    f.write(json.dumps(data_list, indent=3, ensure_ascii=False))